package list;

public class Demo12 {
    public static void main(String[] args) {
        /*
         * 将一个整数转换为7进制
         */
        int n = -66;
        String str = Integer.toString(n, 16);
        System.out.println(str);
        str = toStirng(n,16);
        System.out.println(str);
    }
    public static String toStirng(int n, int radix){
        boolean negative = n<0;
        n = negative ? -n : n;
        char[] digi = {'0','1','2','3','4','5','6','7','8',
                '9','a','b','c','d','e','f'};
        //定义一个数组, 用于存储转换的结果, int 最多转换为32位
        char[] chs = new char[32];
        int index = chs.length-1;
        do{
            int last = n%radix; //取出最后一位数字
            char c = digi[last]; //将数字转换为字符
            chs[index--] = c; //将转换的字符存储到字符数组中
            n /= radix; //去除转换完成的数据
        }while (n!=0);
        if (negative){
            chs[index--] = '-';
        }
        //将转换得到的字符数组, 转换为字符串, 作为结果
        String str = new String(chs, index+1, chs.length-index-1);
        return str;
    }
}
